package com.bee.web.util;

import java.math.BigInteger;

/**
 * 字符串处理工具类
 *
 * @author liuzy
 * @date 2015年6月7日
 */
public class StringUtil {

	public static boolean isNull(String string) {
		return string == null;
	}

	public static boolean hasNull(String... string) {
		for (String str : string) {
			if (isNull(str)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isNullOrEmpty(String string) {
		return string == null || "".equals(string);
	}

	public static boolean hasNullOrEmpty(String... string) {
		for (String str : string) {
			if (isNullOrEmpty(str)) {
				return true;
			}
		}
		return false;
	}

	public static byte[] fromHex(String hex) {
		byte[] bytes = new byte[hex.length() / 2];
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
		}
		return bytes;
	}

	public static String toHex(byte[] bytes) {
		BigInteger bi = new BigInteger(1, bytes);
		String hex = bi.toString(16);
		int paddingLength = (bytes.length * 2) - hex.length();
		if (paddingLength > 0)
			return String.format("%0" + paddingLength + "d", 0) + hex;
		else
			return hex;
	}

}
